package cn.com.hatech.cdkey.module.generate.controller;

import cn.com.hatech.cdkey.common.controller.BaseController;
import cn.com.hatech.cdkey.common.result.RestResponse;
import cn.com.hatech.cdkey.module.generate.entity.CdKey;
import cn.com.hatech.cdkey.module.generate.service.ICdKeyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @projectName: hatech-blog
 * @package: cn.com.hatech.cdkey.module.generate.controller
 * @className: GenerateController
 * @author: xuruikai
 * @description: 生成激活码控制器
 * @date: 2020年2月27日 16:00:52
 * @version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/cd/key")
public class CdKeyRestController extends BaseController {

    private final ICdKeyService cdKeyService;

    @Autowired
    public CdKeyRestController(ICdKeyService cdKeyService) {
        this.cdKeyService = cdKeyService;
    }

    @PostMapping("/generate")
    public RestResponse generate(@RequestBody CdKey cdKey) throws Exception {
        return RestResponse.ok(cdKeyService.generateCode(cdKey), 200);
    }

    @PostMapping("/batch/generate")
    public void batchGenerate(@RequestBody CdKey cdKey, HttpServletResponse response) throws Exception {
        cdKeyService.batchGenerate(response, cdKey);
    }
}
